package cn.itcast.bos.web.action.common;


import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.springframework.data.domain.Page;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@SuppressWarnings("all")
public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>{
	
	//模型驱动
	protected T model;
	@Override
	public T getModel() {
		return model;	
	}
	//构造器完成model 实例化
	public BaseAction(){
		//构造子类Action对象 获取继承父类型的泛型
		Type genericSuperclass = this.getClass().getGenericSuperclass();
		//获取类型第一个泛型参数
		ParameterizedType parmeterizedType=(ParameterizedType) genericSuperclass;
		
		Class<T> modelClass= (Class<T>) parmeterizedType.getActualTypeArguments()[0];
		
		try {
			model = modelClass.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			System.out.println("构造模型失败");
		}
	}
	//接受分页查询参数
		protected int page;
		protected int rows;
		
		
		public void setPage(int page) {
			this.page = page;
		}
		public void setRows(int rows) {
			this.rows = rows;
		}
		//将分页查询结果数据,压入值栈的方法
		protected void pushPageDataToValueStack(Page<T> pageData) {
			//创建Map存入total和rows
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("total", pageData.getTotalElements());
			map.put("rows", pageData.getContent());
			//将map数据压入值栈的最顶层 然后通过json插件取出
			ActionContext.getContext().getValueStack().push(map);
		}
	

}
